<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEditShow = !isEditShow"
      >
        {{ isEditShow ? '完成': '编辑' }}
      </van-button>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(channel, index) in channels"
        :key="channel.id"
        :text="channel.name"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon v-show="isEditShow && !isShowArr.includes(index)" slot="icon" name="close" />
        <span slot="text" class="text" :class="{'active': active === index}">{{ channel.name }}</span>
      </van-grid-item>
    </van-grid>

    <van-cell title="频道推荐" :border="false"></van-cell>
    <van-grid :gutter="10"  class="recommend-grid">
      <van-grid-item
        class="channel-item"
        v-for="value in recommendChannels"
        :key="value.id"
        :text="value.name"
        icon="plus"
        @click="onAddChannel(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/article'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  name: 'channel-edit',
  props: ['channels', 'active'],
  data () {
    return {
      allChannels: [],
      isEditShow: true,
      isShowArr: [0]
    }
  },
  computed: {
    recommendChannels() {
      return this.allChannels.filter(item => !this.channels.find(channel => channel.name === item.name))
    },
    ...mapState(['user'])
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data: { data: {channels} } } = await getAllChannels()
        this.allChannels = channels
      } catch(err) {
        console.log(err)
      }
    },
    async onAddChannel (value) {
      this.channels.push(value)

      if (this.user) {
        try {
          await addUserChannel({
            id: value.id,
            seq: this.channels.length
          })
          this.$toast('添加成功')
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.channels)
      }
    },
    onMyChannelClick (channel, index) {
      if(this.isEditShow) {
        if( index <= this.active ) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.channels.splice(index, 1)

        this.deleteChannel(channel)
      } else {
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.channels)
        }
      } catch (err) {
        console.log(err)
        this.$toast('删除频道失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  .channel-item {
    height: 86px;
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text, .text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
      .active {
        color: red;
      }
    }
  }
  /deep/ .recommend-grid {
    .van-grid-item__content {
      flex-direction: row;
      color: #222;
      .van-icon {
        font-size: 24px;
      }

      .van-grid-item__text {
        font-size: 24px;
        margin-top: 0;
      }
    }
  }

  /deep/ .van-grid-item__icon-wrapper {
    position: unset;
  }

  /deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
  }
}
</style>
